package com.zeng.stpro.modules.security.entity;

import com.zeng.stpro.modules.app.bean.SysUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @ClassName SecurityUser
 * @Description TODO
 * @Author zhijun zeng at 23:33 2021/2/3
 * @Version 1.0
 **/
public class SecurityUser implements UserDetails {

    //当前登录用户
    private transient SysUser currentUserInfo;

    //当前权限
    private List<String> permissionValueList;

    public SecurityUser() {
    }

    public SecurityUser(SysUser user) {
        if (user != null) {
            this.currentUserInfo = user;
        }
    }

    // 重写方法
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        for (String permissionValue : permissionValueList) {
            if(StringUtils.isEmpty(permissionValue)) continue;
            SimpleGrantedAuthority authority = new SimpleGrantedAuthority(permissionValue);
            authorities.add(authority);
        }
        return authorities;
    }

    public SysUser getCurrentUserInfo() {
        return currentUserInfo;
    }

    public void setCurrentUserInfo(SysUser currentUserInfo) {
        this.currentUserInfo = currentUserInfo;
    }

    public List<String> getPermissionValueList() {
        return permissionValueList;
    }

    public void setPermissionValueList(List<String> permissionValueList) {
        this.permissionValueList = permissionValueList;
    }

    @Override
    public String getPassword() {
        return currentUserInfo.getPassword();
    }

    @Override
    public String getUsername() {
        return currentUserInfo.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return currentUserInfo.getNotExpired();
    }

    @Override
    public boolean isAccountNonLocked() {
        return currentUserInfo.getAccountNotLocked();
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return currentUserInfo.getCredentialsNotExpired();
    }

    @Override
    public boolean isEnabled() {
        return currentUserInfo.getEnabled();
    }
}
